# -*- coding: utf-8 -*-
__author__ = 'BuddyHolly'

import pptx
from pptx import Presentation
from app.src.imgsave.curPic import CurPic


# ===================================================================
# *** PPT图片提取 ***
#
# file_info：{'ppt1_path_name': , 'ppt2_path_name': , 'ppt1_page': ,
#            'ppt2_page': , 'pic1_path': , 'pic2_path': }
#
# 返回两ppt提取的name_list
# ===================================================================

def withdraw_pic(file_info):
    # 读入ppt
    prs1 = Presentation(file_info['ppt1_path_name'])
    prs2 = Presentation(file_info['ppt2_path_name'])

    # 选取PPT的slideid
    slide1_id = file_info['ppt1_page'] - 1
    slide2_id = file_info['ppt2_page'] - 1

    # 读取Slide
    slide1 = prs1.slides[slide1_id]
    slide2 = prs2.slides[slide2_id]

    info1_list = []
    info2_list = []

    # 调用InfoPic提取所有图片信息
    for shape_id, shape in enumerate(slide1.shapes):
        if isinstance(shape, pptx.shapes.picture.Picture):
            info1_list.append([slide1_id, shape_id])

    for shape_id, shape in enumerate(slide2.shapes):
        if isinstance(shape, pptx.shapes.picture.Picture):
            info2_list.append([slide2_id, shape_id])

    name1_list = []
    name2_list = []

    # 调用CurPic存储图片
    for i in range(info1_list.__len__()):
        current1_pic = CurPic(prs1, info1_list[i], file_info['pic1_path'])
        current1_pic.get_pic()
        name1_list.append(current1_pic.file_name)
        current2_pic = CurPic(prs2, info2_list[i], file_info['pic2_path'])
        current2_pic.get_pic()
        name2_list.append(current2_pic.file_name)

    # 返回两ppt提取的name_list
    return name1_list, name2_list
